package com.informationsystem.service.impl;

import com.informationsystem.dao.NewsDao;
import com.informationsystem.model.News;
import com.informationsystem.model.PictureNews;
import com.informationsystem.service.NewsManager;
import com.informationsystem.service.impl.GenericManagerImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.jws.WebService;

@Service("newsManager")
@WebService(serviceName = "NewsService", endpointInterface = "com.informationsystem.service.NewsManager")
public class NewsManagerImpl extends GenericManagerImpl<News, Long> implements NewsManager {
    NewsDao newsDao;

    @Autowired
    public NewsManagerImpl(NewsDao newsDao) {
        super(newsDao);
        this.newsDao = newsDao;
    }

	@Override
	public List getNewsList() {
		List<News> newsList = getAll();
		Collections.sort(newsList, new PostDateDsc());
		return newsList;
	}
	
	private  class PostDateDsc implements  Comparator<News>{
		@Override
		public int compare(News o1, News o2) {
			return o2.getPostDate().compareTo(o1.getPostDate());
		}
	}

	@Override
	public List getTop4NewsList() {
		List newsList = getNewsList();
		if(newsList.size()>4)
			newsList = newsList.subList(0, 4);
		return newsList;
	}

	@Override
	public News getTop1Picturenews() {
		List<News> newsList = getNewsList();
		for(News n : newsList){
			if(n.getPicturenews()!=null)
				return n;
		}
		return new News();
	}
}